<script setup>
import MoonNav from '@/components/MoonNav.vue';
import MoonBaner from '@/components/MoonBaner.vue';
import { blogOne } from '@/api/bloglist';
import { arrowMiddleware } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import { ref } from 'vue'
import { useUserStore } from '@/store/user.js'
import { blogdel } from '@/api/bloglist';
const UserStore = useUserStore()
const route = useRoute()
const router = useRouter()
// 渲染
let datas = ref('')
let resid = ref('')
const render = async () => {
  const res = await blogOne(route.query.id)
  console.log(res.data)
  datas.value = res.data

  resid.value = res.data.userId
  // console.log(res.data.userId, '请求的')
}
render()
//删除
const dels = async () => {
  try {
    const res = await blogdel({ id: datas.value._id })
    console.log(res)
    if (res.code === 200) {
      ElNotification({
        title: 'Success',
        message: res.msg,
        type: 'success',
      })
      router.push('/home')
    }
  } catch (err) {

  }
}
// 用户判断
const userid = ref(UserStore.user.id)
console.log(userid, '用户的')


</script>
<template>
  <MoonNav></MoonNav>
  <MoonBaner></MoonBaner>
  <!-- 头部 -->
  <div class="details">
    <div class="detop">
      <!-- <img v-loadImg="item.imgs" alt=""> -->
      <img v-loadImg="datas.imgs || datas.imgs">
    </div>
    <!-- 底部 -->
    <div class="debottom">
      <!-- 作者 -->
      <div class="author">
        <i class="iconfont icon-d"></i>
        <div>作者:{{ datas.userName || datas.userId }}</div>
      </div>

      <!-- 标题 -->
      <div class="title">
        <p>{{ datas.title }}</p>
      </div>
      <p class="context" v-html="datas.content">

      </p>
      <div class="detime">
        更新时间 <span>{{ new Date(datas.updatedAt).toLocaleString() }}</span>
      </div>
      <div> 文章类型 {{ datas.tags[0] }}</div>
    </div>
  </div>
  <!-- 留言 -->
  <div class="vgn" v-if="userid === datas.userId">
    <el-button type="danger"
      @click="router.push({ path: '/ed', query: { id: datas._id, content: datas.content, title: datas.title, tags: datas.tags } })">编辑</el-button>
    <el-button type="primary" @click="dels">删除</el-button>
  </div>
  <div class="tolevae">
    <div class="leavehd">
      <textarea name="" id=""></textarea>
      <button>评论</button>
    </div>
    <div class="leavebd">
      <div class="leitem" v-for="item in datas.comments">
        <div class="text">
          <img :src="datas.Avatar" alt="">
          <div class="box">
            <div class="user">{{ item.commenterId }}</div>
            <p>{{ item.commentText }}</p>
            <span>时间 {{ item.timestamp }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>



<style lang="less" scoped>
.details {
  width: 60%;
  // height: 1100px;
  margin: 30px auto;
  color: #000;
  font-family: '楷体';

  .detop {
    width: 100%;
    height: 400px;
    // background: orange;
    margin: 0px auto;
    display: flex;
    justify-content: center;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .debottom {
    width: 100%;
    // background: pink;

    // 作者
    .author {
      width: 100%;
      height: 30px;
      padding: 30px 0px;
      display: flex;
      font-size: 18px;
      justify-content: start;
      align-items: center;
      color: #000;
      border-bottom: 1px solid #ccc;
      font-weight: 600;

      i {
        font-size: 18px;
        padding-right: 10px;
      }
    }




    // 标题
    .title {
      width: 100%;
      height: 30px;
      margin: 20px 0px;
      font-size: 18px;
    }

    // 文本
    .context {
      width: 100%;
      font-size: 16px;
      text-indent: 2em;
    }

    // 时间
    .detime {
      width: 100%;
      height: 20px;
      margin: 20px 0px;
    }
  }



}

.vgn {
  width: 180px;
  height: 30px;
  margin: 0px auto;
}


.tolevae {
  width: 60%;
  margin: 80px auto;

  .leavehd {
    width: 100%;
    height: 100px;
    display: flex;

    textarea {
      width: 90%;
      height: 100px;
      padding: 10px;
      resize: none;
      border: 1px solid #ccc;
      outline: none;
      box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.276);

    }

    button {
      display: block;
      width: 10%;
      height: 100px;
      outline: none;
      border: 0px;
      color: #9a9898;
      font-size: 20px;
      box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.276);
    }
  }

  .leavebd {
    width: 100%;
    margin-top: 20px;

    .leitem {
      max-width: 650px;
      height: 100px;
      box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.276);
      margin: 20px 0px;
      border-radius: 5px;

      .text {
        display: flex;

        img {
          width: 50px;
          height: 50px;
          margin: 5px;
        }

        .box {
          margin-top: 5px;

          .user {
            margin-bottom: 10px;
            font-size: 16px;
          }

          span {
            display: block;
            margin-right: auto;
            margin-top: 20px;
          }
        }
      }
    }
  }
}
</style>